<template>
  <div class="wrap">
    <div class="header">
      <div class="type-wrap" @click="PopTypeRefToggle">
        <span class="title">{{ currentSelect.name || '全部类型' }}</span>
        <van-icon name="apps-o" />
      </div>
      <div class="data-wrap">
        <span class="time" @click="PopMonthRefToggle"
          >{{ currentTime }}<van-icon name="arrow-down" />
        </span>
        <span class="expense">总支出 ¥{{ totalExpense }}</span>
        <span class="income">总收入 ¥{{ totalIncome }}</span>
      </div>
    </div>
    <div class="content-wrap">
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list
          v-model:loading="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <CardItem v-for="(v, i) in list" :key="i" :bill="v"></CardItem>
        </van-list>
      </van-pull-refresh>
    </div>
    <div class="add" @click="addToggle">
      <van-icon name="records" />
    </div>
    <PopType ref="PopTypeRef" @selectType="selectType"></PopType>
    <PopMonth ref="PopMonthRef" @selectTime="selectTime"></PopMonth>
    <PopAdd ref="PopAddRef" @refresh="onRefresh"></PopAdd>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { reactive, toRefs, onMounted, ref } from 'vue'
import { post, get } from '@/utils/request'
import { Toast } from 'vant'
import CardItem from '@/components/CardItem'
import PopType from '@/components/PopType'
import PopMonth from '@/components/PopMonth'
import PopAdd from '@/components/PopAdd'
export default {
  name: 'home',
  components: {
    CardItem,
    PopType,
    PopMonth,
    PopAdd
  },
  setup() {
    const PopTypeRef = ref(null)
    const PopMonthRef = ref(null)
    const PopAddRef = ref(null)
    const state = reactive({
      currentSelect: {},
      currentTime: dayjs().format('YYYY-MM'),
      totalExpense: 0.0,
      totalIncome: 0.0,
      totalPage: 0,
      page: 0,
      page_size: 5,
      list: [],
      loading: false,
      finished: false,
      refreshing: false
    })
    onMounted(async () => {})
    const monthToggle = () => {}
    const getBillList = async () => {
      if (state.refreshing) {
        state.list = []
        state.refreshing = false
      }
      const params = {
        date: state.currentTime,
        page: state.page,
        page_size: state.page_size,
        type_id: state.currentSelect.id || 'all'
      }
      const { data } = await get('/bill/list', params)
      state.totalExpense = data.totalExpense
      state.totalIncome = data.totalIncome
      state.totalPage = data.totalPage
      state.list = state.list.concat(data.list)
      state.loading = false
      if (state.page >= state.totalPage) {
        state.finished = true
      }
    }
    const onLoad = () => {
      state.page = state.page + 1
      console.log(state.page)
      getBillList()
    }
    const onRefresh = () => {
      state.finished = false
      state.page = 0
      state.loading = true
      state.refreshing = true
      onLoad()
    }
    //类型弹窗切换
    const PopTypeRefToggle = () => {
      PopTypeRef.value.toggle()
    }
    //时间弹窗切换
    const PopMonthRefToggle = () => {
      PopMonthRef.value.toggle()
    }
    //时间弹窗切换
    const addToggle = () => {
      PopAddRef.value.toggle()
    }
    //选择类型
    const selectType = (item) => {
      state.currentSelect = item
      onRefresh()
    }
    //选择时间
    const selectTime = (item) => {
      state.currentTime = item
      onRefresh()
    }
    return {
      ...toRefs(state),
      onLoad,
      onRefresh,
      PopTypeRef,
      PopMonthRef,
      PopAddRef,
      PopTypeRefToggle,
      PopMonthRefToggle,
      addToggle,
      selectType,
      selectTime
    }
  }
}
</script>

<style lang="less" scoped>
.wrap {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding-top: 80px;
  .header {
    position: fixed;
    top: 0;
    left: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 100%;
    height: 80px;
    background-color: var(--bg-color3);
    color: #fff;
    font-size: 14px;
    padding: 20px 20px;
    z-index: 100;
    box-sizing: border-box;
    .type-wrap {
      background-color: var(--bg-color3);
      display: inline-block;
      padding: 6px;
      border-radius: 4px;
      position: relative;
      align-self: baseline;
      border: 1px solid var(--border-color);
      .title {
        margin-right: 22px;
      }
      .title::after {
        content: '';
        position: absolute;
        top: 12px;
        bottom: 11px;
        right: 32px;
        width: 1px;
        background-color: #e9e9e9;
      }
    }
    .data-wrap {
      margin-top: 10px;
      font-size: 13px;
      .time {
        margin-right: 12px;
        .sort-down {
          font-size: 12px;
        }
      }
      .expense {
        margin-right: 10px;
      }
    }
  }
  .content-wrap {
    width: 100%;
    height: 100%;
    padding: 10px;
    box-sizing: border-box;
    overflow-y: auto;
  }
  .add {
    position: fixed;
    bottom: 100px;
    right: 30px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid #e9e9e9;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 20px;
    background-color: #fff;
    box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
    color: var(--text-color5);
  }
}
</style>
